﻿using HuaCheng.CommonClass;
using HuaCheng.Controller;
using HuaCheng.DataAccess;
using HuaCheng.Model;
using HuaCheng.UserControls;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;

namespace HuaCheng.Views
{
    /// <summary>
    /// 通道映射
    /// </summary>
    public partial class ChannelMap : Form
    {
        #region 全局参数
        public static string devIp;//当前设备id
        public static int GID;//柜号
        public static int MID;//中位机号
        //private string devTypeName;//设备类型名称
        private int count;//节点只在第一次双击刷新页面
        public delegate void weekIndex(bool connected,bool sizechange);//委托
        public delegate void weekBatcode();
        public event weekIndex refreshIndexpage;//事件 在关闭当前页面时委托刷新indexpage
        public event weekBatcode refreshBatcode;
        public bool[] exisStat = new bool[8];//记录每个通道是否存在 
        public int mapCount;//每排映射个数
        public string path;
        public bool flag;//是否被全部清空
        public int oldMap;//重置映射前的每行通道数
        public delegate void UpdateChlAterNewMap(int map,List<Channel> cahnnels);//委托 更新通道数据类
        public event UpdateChlAterNewMap NewChannels;//事件 
        public bool isSave;//是否保存
        public static Dictionary<int,bool> oldIds;//原通道映射未被撤销的通道realId集合
        public static Dictionary<int, bool> newIds;//修改后的映射未被撤销的通道realId集合
        public static List<Channel> channels;//通道信息
        public delegate void getAuxNum(Dictionary<int, string> allAuxNum);//辅助通道映射信息委托和事件
        public static Dictionary<int, string> mapDic = new Dictionary<int, string>();//全局静态主通道和辅助通道的映射关系字典
        public static List<int> revokeChl = new List<int>();//全局静态撤销通道号
        #endregion
        #region 页面初始化
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="devName">设备名称</param>
        /// <param name="devTypeName">设备类型名称</param>
        /// <param name="ip">设备号</param>
        public ChannelMap(string ip,int gid,int mid,int oldMapCount,List<Channel> chls)
        {
            InitializeComponent();
            oldIds = new Dictionary<int, bool>();
            newIds = new Dictionary<int, bool>();
            channels = chls;
            devIp = ip;//设备IP
            GID = gid;
            MID = mid;
            InitTreeView(ip,gid,mid);
            BindRevoMenu();//为底层设置空通道绑定撤销通道菜单使能
            path = DicHelper.RetrieveConfigPath();//获取配置文件路径
            flag = false;
            oldMap = oldMapCount;
            mapCount = ChannelMapControl.GetDevChlMapLine(devIp,gid,mid);//读取每行排列个数
            this.FormClosing += new FormClosingEventHandler(this.ChannelMap_Closing);
            initTsmiAuxMap();
        }
        /// <summary>
        /// 如果当前有主通道映射，那么右键菜单辅助通道映射可用
        /// </summary>
        private void initTsmiAuxMap()
        {
            if (ChlMapHelper.GetDevChlMap(devIp,GID,MID).Count > 0)
            {
                tsmiAuxChl.Enabled = true;
            }
        }
        /// <summary>
        /// 重写重置映射页面窗口关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChannelMap_Closing(object sender, FormClosingEventArgs e)
        {

            if (CompareChange()&&!isSave)//如果进行了重置且未保存
            {
                DialogResult chose = new DialogResult();
                if (IndexPage.language == "en-US")
                {
                    chose = MessageBox.Show("Do you want to save?", "Tip", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                }
                else
                {
                    chose = MessageBox.Show("您有修改尚未保存，是否进行保存?", "系统提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                }
                if ( chose == DialogResult.Yes)
                {
                    SaveNewMap();
                    e.Cancel = false;
                }
                else if (chose == DialogResult.No)
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                if (IndexPage.language == "en-US")
                {
                    if (MessageBox.Show("Make sure to exit？", "Tip", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
                else
                {
                    if (MessageBox.Show("你确定要退出？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            }              
        }
        /// <summary>
        /// 显示左侧节点
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        private void InitTreeView(string t1,int t2,int t3)
        {
            TreeNode tnDev = new TreeNode();//具体设备节点
            TreeNode tnDevTy = new TreeNode();//设备类型节点
            TreeNode rootNode = new TreeNode();//根节点
            tnDev.Text = t3 + "(" + t1 + ")";//中位机号+IP
            tnDev.Name = "device" + t1;
            tnDevTy.Text = t2.ToString();//柜号
            tnDevTy.Name = "devType" + t2;
            tnDevTy.Nodes.Add(tnDev);
            rootNode = tvwDevList.Nodes[0];//找到根节点
            rootNode.Text = IndexPageControl.GetClientLocalIPv4Address();
            rootNode.Nodes.Add(tnDevTy);
            //给节点绑定ImageList
            ImageList myImageList = new ImageList();
            string imgpath = DicHelper.RetrieveNodeImgPath();
            imgpath = imgpath + "node.jpg";
            myImageList.Images.Add(Image.FromFile(imgpath));
            tvwDevList.ImageList = myImageList;
            tvwDevList.ImageIndex = 0;
            count = 0;
        }
        /// <summary>
        /// 为datagridview添加自定义的列类型
        /// </summary>
        private void InitDataGridView()
        {
            ChannelMapControl.AddDgvColumn(dgvChlInfo);
            for (int i = 0; i < 8; i++)
            {
                string chlName = "chlSim" + (i + 1);
                ChannelSimple chs = (ChannelSimple)this.pnlContent.Controls[chlName];
                this.dgvChlInfo.Rows.Add(new object[] { i + 1, ResourceCulture.GetString("MainChannel") });//添加8个通道
                if (chs.Visible)//可见即通道存在
                {
                    exisStat[i] = true;//显示黄色图像
                }
                else
                {
                    exisStat[i] = false;//显示灰色图像
                }
            }
        }        
        /// <summary>
        /// 显示通道映射信息
        /// </summary>
        /// <param name="bts"></param>
        private void ShowMap(List<BTSChlMap> bts)
        {
            if (bts.Count > 0 && mapCount > 0)//存在通道映射信息
            {
                int unrev = 0;
                foreach (BTSChlMap bt in bts)
                {
                    int rid = bt.ChlRealId;
                    string chlSimName = "chlSim" + (rid + 1);
                    ChannelSimple chlMap = (ChannelSimple)this.pnlContent.Controls[chlSimName];//获取控件
                    int CID= bt.ChlMapId;//映射通道 
                    chlMap.SetParam(GID,MID,CID, rid);//设置参数
                    chlMap.Visible = !bt.RevocationStatus;//未被撤销即可见
                    chlMap.BringToFront();//浮出第一层
                    chlMap.ChlRightMouseClick += new ChannelSimple.ChlRevert(SetMenuEnable);//订阅方法
                    oldIds.Add(bt.ChlRealId, bt.RevocationStatus);
                    if (!bt.RevocationStatus)
                    {
                        unrev++;
                    }
                }
                //AuxChlShow();
                if(unrev>0)
                    this.tsmiClearAll.Enabled = true;//设置全部清空按钮可用
            }
        }
        /// <summary>
        /// 列格式化 根据每个通道显示的状态 把对应图片和文字显示在同一列
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvChlInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //格式化第一列
            if (e.ColumnIndex == 0)
            {
                TextAndImageCell cell = dgvChlInfo.Rows[e.RowIndex].Cells[e.ColumnIndex] as TextAndImageCell;
                if (cell != null && e.Value != null)
                {
                    try
                    {
                        string path = DicHelper.RetrieveNodeImgPath();
                        if (exisStat[e.RowIndex])
                            {
                                path += "bat1.jpg";
                            }
                            else
                            {
                                path += "bat2.jpg";
                            }
                            cell.Image = GetImage(path);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message + ex.StackTrace);
                    }
                }
            }
        }
        #endregion
        #region 右键菜单
        /// <summary>
        /// 一级菜单项 撤销当前通道
        /// 1.隐藏当前通道
        /// 2.修改数据表中的图片为灰色
        /// 3.恢复“保存映射”菜单可用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiRevCurChl_Click(object sender, EventArgs e)
        {
            string chlName = ((sender as ToolStripMenuItem).Owner as ContextMenuStrip).SourceControl.Name;//获取源触发控件
            int realId = IndexPageControl.GetRealIdByName(chlName)-1;//获取id
            bool isrun = channels[realId].LockedStat || channels[realId].RunStat || channels[realId].StopStat;//运行中、停止、锁定等状态不可以撤销通道
            if (!isrun)
            {
                this.pnlContent.Controls[chlName].Visible = false;//设置通道不可见即撤销通道
                int rowIndex = realId;//((ChannelSimple)this.pnlContent.Controls[chlName]).ChlRealId;//realId对应行号
                TextAndImageCell cell = dgvChlInfo.Rows[rowIndex].Cells[0] as TextAndImageCell;//获取该通道对应的行列
                exisStat[rowIndex] = false;//换灰色图片
                cell.Value = rowIndex;//触发dgvChlInfo_CellFormatting函数
                this.tsmiSaveMap.Enabled = true;//恢复保存映射菜单
                tsmiAverage.Enabled = false;
                revokeChl.Add(realId);//更新撤销的主通道List
               // mapDic.Clear();//清空主通道辅助通道映射字典
                mapDic[realId+1] = "";//清空被撤销的主通道辅助通道映射字典
            }
        }
        /// <summary>
        /// 一级菜单项 全部清空
        /// 1.所有的通道不可见 
        /// 2.修改数据表中的全部图片为灰色
        /// 3.“全部清空”按钮不可用
        /// 4.恢复“保存映射”菜单可用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiClearAll_Click(object sender, EventArgs e)
        {
            Boolean hasrunaux = false;
            DialogResult result = new DialogResult();
            if (IndexPage.language == "en-US")
            {
                result = MessageBox.Show("Make sure to delete all？", "Tip", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            else
            {
                result = MessageBox.Show("确认清空？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            }
            if(result == DialogResult.OK)
            {
                foreach (Control ctr in this.pnlContent.Controls)
                {
                    if (ctr is ChannelSimple)
                    {
                        ChannelSimple chs = ctr as ChannelSimple;
                        int realId = IndexPageControl.GetRealIdByName(chs.Name)-1;//获取id
                        bool isrun = channels[realId].LockedStat || channels[realId].RunStat || channels[realId].StopStat;//运行中、停止、锁定等状态不可以撤销通道
                        if (!isrun)
                        {
                            chs.Visible = false;//设置通道不可见即撤销通道
                            int rowIndex = chs.ChlRealId;//realId对应行号
                            TextAndImageCell cell = dgvChlInfo.Rows[rowIndex].Cells[0] as TextAndImageCell;//获取该通道对应的行列
                            exisStat[rowIndex] = false;//换灰色图片
                            cell.Value = rowIndex;//触发dgvChlInfo_CellFormatting函数
                            this.tsmiSaveMap.Enabled = true;//恢复保存映射菜单
                        }
                        else
                        {
                            hasrunaux = true;
                        }
                    }
                }
                //存在不可撤销通道增加提示信息
                if (IndexPage.language == "en-US")
                {
                    if (hasrunaux)
                    {
                        MessageBox.Show("Channel doesn't clear all！");
                    }
                }
                else
                {
                    if (hasrunaux)
                    {
                        MessageBox.Show("当前存在不可撤销（运行中、停止、锁定等状态）的通道，通道没有全部清空！");
                    }
                }
                ToolStripMenuItem ts = sender as ToolStripMenuItem;
                ts.Enabled = false;//当前菜单项不可用
                this.tsmiSaveMap.Enabled = true;//恢复保存映射菜单
                flag = true;
                tsmiAuxChl.Enabled = false;//点击全部清除后，辅助通道菜单不可用
                //清空辅助通道映射对象
                for (int i = 0; i < 8; i++)
                {
                    int strKey = (i + 1);
                    mapDic[strKey] = "";
                }
                //更新撤销主通道List
                revokeChl.Clear();
                for (int i = 0; i < 8; i++)
                {
                    revokeChl.Add(i);
                }
            }
        }
        /// <summary>
        /// 二级菜单 设置主通道默认映射 1 x 8
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiDefMapFst_Click(object sender, EventArgs e)
        {
            mapCount = 8;
            SetDefaMap(mapCount);
            //保存映射菜单亮
            tsmiSaveMap.Enabled = true;
            //平均映射亮
            tsmiAverage.Enabled = true;
            //点击主通道映射1x8后，辅助通道映射亮
            tsmiAuxChl.Enabled = true;
            //清空辅助通道映射对象
            for (int i = 0; i < 8; i++)
            {
                int strKey = (i + 1);
                mapDic[strKey] = "";
            }
            AuxChlShow();
            //更新撤销的主通道List
            revokeChl.Clear();
        }
        
        /// <summary>
        /// 一级菜单 保存映射
        /// 1.修改保存到数据库表
        /// 2.把当前设备排布个数存到配置文件
        /// 3.如果全部清空了映射，则要删除原来通道锁定的xml文件
        /// 4.刷新主界面
        /// 5.关闭当前通道映射页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiSaveMap_Click(object sender, EventArgs e)
        {
            SaveNewMap();
        }
        /// <summary>
        /// 保存映射
        /// </summary>
        private void SaveNewMap()
        {
            ChannelMapControl.DeleteDev(devIp,GID,MID);//删除原有的通道映射信息
            string xmlName = "Lock-" + devIp + ".xml";//根据下拉框内容查询相应的xml文件
            string xmlPath = DicHelper.RetrieveChlLockPath() + xmlName;//xml文件路径
            for (int i = 0; i < 8; i++)
            {
                string chlName = "chlSim" + (i + 1);
                ChannelSimple chs = (ChannelSimple)this.pnlContent.Controls[chlName];
                if (ChannelMapControl.queryMap(devIp, chs))
                {
                    ChannelMapControl.ModifyAuxMap(devIp,chs, mapDic[(i + 1)]);
                }
                else
                {
                    ChannelMapControl.ModifyDevMap(devIp,GID,MID, chs, mapDic[(i + 1)]);
                }
                channels[i].Cid = i % mapCount + 1;//修改映射通道号
                channels[i].Mid = MID;//修改柜号
                channels[i].ChId = (MID) + "-" + (i % mapCount + 1);
            }
            foreach (int key in mapDic.Keys)
            {
                if (mapDic[key] != "")
                {
                    List<string> oneChlAux = mapDic[key].Split(',').ToList();
                    List<AuxChannel> auxList = new List<AuxChannel>();
                    foreach (string id in oneChlAux)
                    {
                        int auxId = Convert.ToInt32(id);
                        AuxChannel aux = new AuxChannel();
                        aux.auxId = auxId;
                        aux.temperature1 = 0.0f;
                        aux.temperature2 = 0.0f;
                        aux.temperature3 = 0.0f;
                        aux.voltage = 0.0f;
                        auxList.Add(aux);
                    }
                    channels[key-1].auxChannel = auxList;
                }
                else
                {
                    channels[key - 1].auxChannel = new List<AuxChannel>();
                }

            }
            NewChannels(mapCount,channels);//触发通道数据类更新
            isSave = true;
            refreshIndexpage(true,false);//关闭前刷新主页面
            refreshBatcode();//刷新电池条码
            //this.Close();
        }
        #endregion
        #region 控件点击事件
        /// <summary>
        /// 树形图设备节点双击 显示通道映射图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvwDevList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (count < 1 && e.Node.Name.Contains("device"))//只有在设备节点上双击
            {
                List<BTSChlMap> bts = ChannelMapControl.GetDevChlMap(devIp,GID,MID);//获取通道映射表
                ShowMap(bts);//显示通道映射
                this.pnlContent.ContextMenuStrip = ctxmsRstChl;//双击节点之后给右侧区域绑定菜单
                tsmiMainDefMap.Enabled = true;//允许点击主通道映射
                tsmiDefMapFst.Enabled = true;
                InitDataGridView();//加载数据表
                count++;
                //将数据库中主通道和辅助通道的映射关系赋值给mapDic
                mapDic = ChlMapHelper.queryOwnAux(devIp,GID,MID);
                //如果数据库没有辅助通道映射信息
                if (mapDic == null)
                {
                    //初始化mapDic
                    for (int i = 0; i < 8; i++)
                    {
                        int strKey = (i + 1);
                        mapDic[strKey] = "";
                    }
                }
                //如果数据库有辅助通道映射信息
                else
                {
                    for (int i = 0; i < 8; i++)
                    {
                        if (!mapDic.ContainsKey((i + 1)))
                        {
                            mapDic.Add((i + 1), "");
                        }
                    }
                }
                AuxChlShow();
                revokeChl = ChlMapHelper.QueryRevokeChl(devIp,GID,MID);//查询撤销的通道号
            }
        }
        /// <summary>
        /// 容器中右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pnlContent_MouseDown(object sender, MouseEventArgs e)
        {
            SetMenuUnEnable();
        }
        #endregion
        #region 设置方法
        /// <summary>
        /// 设置右键菜单第一项可用
        /// </summary>
        private void SetMenuEnable()
        {
            this.tsmiRevCurChl.Enabled = true;
        }
        /// <summary>
        /// 设置右键菜单第一项不可用
        /// </summary>
        private void SetMenuUnEnable()
        {
            this.tsmiRevCurChl.Enabled = false;
        }
        /// <summary>
        /// 设置通道排版 1 x 8 
        /// 设置相应通道参数
        /// 改变datagrid相应图片
        /// </summary>
        /// <param name="n">每行排多少个</param>
        private void SetDefaMap(int n)
        {
            for (int i = 0; i < 8; i++)
            {
                string chlName = "chlSim" + (i+1);
                ChannelSimple chs = new ChannelSimple();
                chs = (ChannelSimple)this.pnlContent.Controls[chlName];
                //int x = 4 + i % n * 149;//计算横坐标
                //int y = 4 + i / n * 154;//计算纵坐标
                ChannelBlank chlBlank = (ChannelBlank)this.pnlContent.Controls["chlBlank"+(i+1)];
                int x = chlBlank.Location.X;//计算横坐标
                int y = chlBlank.Location.Y;//4+327*rid / mapCount+26*(rid / mapCount)计算纵坐标            
                //int uid = i / n + 1;//柜号
                int cid = i % n + 1;//映射通道
                int rid = i;//每次重新设置原来的realid会丢失               
                chs.Location = new Point(x, y);
                chs.SetParam(GID, MID,cid, rid);//位置变了重新设置参数
                chs.ChlRightMouseClick += new ChannelSimple.ChlRevert(SetMenuEnable);//订阅方法
                chs.Visible = true;
                chs.BringToFront();//浮出第一层
                int rowIndex = chs.ChlRealId;//realId对应行号
                TextAndImageCell cell = dgvChlInfo.Rows[rowIndex].Cells[0] as TextAndImageCell;//获取该通道对应的行列
                exisStat[rowIndex] = true;//换灰色图片
                cell.Value = rowIndex;//触发dgvChlInfo_CellFormatting函数
            }
            this.tsmiClearAll.Enabled = true;//恢复全部清空菜单
            this.tsmiSaveMap.Enabled = true;//恢复保存映射菜单
        }
        /// <summary>
        /// 根据图片路径返回相应图片
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public Image GetImage(string path)
        {
            return Image.FromFile(path);
        }
        /// <summary>
        /// 在空白通道上右键撤销通道菜单不可用
        /// </summary>
        private void BindRevoMenu()
        {
            for (int i = 0; i < 8;i++ )
            {
                string chlName = "chlBlank" + (i + 1);
                ChannelBlank chb = (ChannelBlank)this.pnlContent.Controls[chlName];
                chb.ChlMapId = MID + "-" + (i % 8 + 1);
                chb.ChlRightMouseClick += new ChannelBlank.ChlRevert(SetMenuUnEnable);//给每个空白通道订阅右键监听事件 
            }
        }
        /// <summary>
        /// 获取最后的通道映射信息
        /// </summary>
        public void GetLastMap()
        {
            newIds.Clear();
            for (int i = 0; i < 8; i++)
            {
                string ubName="chlSim"+(i+1);
                bool isRevo = !this.pnlContent.Controls[ubName].Visible;//如果通道不可见则代表该通道被撤销
                newIds.Add(i,isRevo);
            }
        }
        /// <summary>
        /// 对比通道映射有无发生改变
        /// </summary>
        /// <returns></returns>
        public bool CompareChange()
        {
            if (oldMap != mapCount)
            {
                return true;
            }
            else
            {
                GetLastMap();
                foreach (int item in oldIds.Keys)
                {
                    if (oldIds[item] != newIds[item])
                    {
                        return true;
                    }
                }
                return false;
            }

        }
        #endregion
        #region 辅助通道
        /// <summary>
        /// 找出当前页面所有通道中主通道下最多的辅助通道个数
        /// </summary>
        /// <returns></returns>
        public int GetMaxAuxCount()
        {
            int maxAuxCount = 0;
            for (int i = 1; i <= 8; i++)
            {
                string auxchls = mapDic[i];
                string[] auxIds = auxchls.Split(',');
                int index=0;
                if (auxIds[index] != "" && maxAuxCount < auxIds.Length)
                {
                    maxAuxCount = auxIds.Length;
                }
            }
            return maxAuxCount;
        }
        /// <summary>
        /// 平均映射
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiAverage_Click(object sender, EventArgs e)
        {
            bool isrun=false;
            foreach (Control ctr in this.pnlContent.Controls)
            {
                if (ctr is ChannelSimple)
                {
                    ChannelSimple chs = ctr as ChannelSimple;
                    int realId = IndexPageControl.GetRealIdByName(chs.Name)-1;//获取id
                    isrun = channels[realId].LockedStat || channels[realId].RunStat || channels[realId].StopStat;//运行中、停止、锁定等状态不可以撤销通道
                    if(isrun)
                    {
                        if (IndexPage.language == "en-US")
                        {
                            MessageBox.Show("Some channels is running，average mapping failed！");
                        }
                        else
                        {
                            MessageBox.Show("当前存在正在运行的通道，无法平均映射！");
                        }
                         return;
                    }
                }
            }
            if (revokeChl.Count == 0)
            {
                for (int i = 0; i < 8; i++)
                {
                    mapDic[i + 1] = (i + 1).ToString();
                }
                tsmiSaveMap.Enabled = true;
                AuxChlShow();
            }
            else
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Some channels is undid，average mapping failed！");
                }
                else
                {
                    MessageBox.Show("当前存在撤销的通道，无法平均映射！");
                }
            }
        }
        /// <summary>
        /// 自定义菜单点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiCustom_Click(object sender, EventArgs e)
        {
            AuxCustom auxCus = new AuxCustom(devIp);
            auxCus.setAuxChl += new AuxCustom.showAuxChl(AuxChlShow);
            auxCus.Owner = this;
            auxCus.StartPosition = FormStartPosition.CenterScreen;
            auxCus.ShowDialog();
            tsmiSaveMap.Enabled = true;
        }
        /// <summary>
        /// 显示辅助通道
        /// </summary>
        public void AuxChlShow()
        {
            int maxAuxCount = GetMaxAuxCount();
            for (int i = 1; i <= 8; i++)
            {
                string auxchls = mapDic[i];
                string name = "chlSim" + i;
                ChannelSimple chs = (ChannelSimple)this.pnlContent.Controls[name];
                ChannelBlank chb = (ChannelBlank)this.pnlContent.Controls["chlBlank" + i];
                chs.Height = chs.Controls["txb1"].Location.Y + maxAuxCount * (chs.Controls["txb1"].Height + 6) + 3;
                chb.Height = chs.Height;
                string[] auxIds = auxchls.Split(',');
                int index=0;
                for (int j = 1; j <=8; j++)
                {
                    string auxName = "txb" + j;
                    if (index<auxIds.Length)
                    {
                        if(auxIds[index] != "")
                        {
                            //auxName = "txb" + auxIds[index];
                            chs.Controls[auxName].Text = ResourceCulture.GetString("AuxChannel") + auxIds[index];
                            chs.Controls[auxName].Visible = true;
                            index++;
                        }
                        else
                        {
                            chs.Controls[auxName].Visible = false;
                        }
                    }
                    else
                    {
                        chs.Controls[auxName].Visible = false;
                    }

                }
            }
        }
        #endregion
        #region 国际化
        private void ChannelMap_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("SetMapping");
            this.tsmiRevCurChl.Text = ResourceCulture.GetString("UndoCurrentChannel");
            this.tsmiClearAll.Text = ResourceCulture.GetString("ClearAll");
            this.tsmiSaveMap.Text = ResourceCulture.GetString("SaveMap");
            this.tsmiMainDefMap.Text = ResourceCulture.GetString("MainDefMap");
            this.tsmiAuxChl.Text = ResourceCulture.GetString("AuxChlMap");
            this.tsmiAverage.Text = ResourceCulture.GetString("AverageMap");
            this.tsmiCustom.Text = ResourceCulture.GetString("CustomMap");
        }
        #endregion
    }
}
